package com.origin.uilibrary.dailog.bese;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Build;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.util.SparseArray;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.FloatRange;
import androidx.annotation.StyleRes;
import androidx.appcompat.app.AppCompatDialog;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;

import com.origin.uilibrary.R;
import com.origin.uilibrary.SelectorFactory;
import com.origin.uilibrary.databinding.DialogBaseCenterBinding;
import com.origin.uilibrary.utils.Utils;

public abstract class CenterBaseVBDialog<VB extends ViewDataBinding> extends AppCompatDialog {
    private SparseArray<View> views = new SparseArray<>();
    private boolean isFullscreen = false;//是否全屏
    private float mDimAmount = 0.5f;//背景昏暗度
    private boolean mShowBottomEnable;//是否底部显示
    private int mMargin = 0;//左右边距
    private int mAnimStyle = 0;//进入退出动画
    private boolean mOutCancel = true;//点击外部取消
    public Context mContext;
    private int mWidth;
    private int mHeight;


    public CenterBaseVBDialog(Context context) {
        super(context, R.style.BaseDialogStyle);

        this.mContext = context;
    }

    public void doAction() {
        int padding = (int) (Utils.statusBarHeight(mContext) * 1.0);
        dialogViewBinding.getRoot().setPadding(0, padding, 0, padding);
    }


    public void setOnCancelListenerOut(OnCancelListener cancelListener) {
        this.setOnCancelListener(cancelListener);
    }


    private VB dialogViewBinding;

    public VB getDialogViewBinding() {
        return dialogViewBinding;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Window window = getWindow();
        if (window != null) {
            window.clearFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
            window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE | WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);

            window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
            window.setStatusBarColor(Color.TRANSPARENT);
            window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
        }
        requestWindowFeature(Window.FEATURE_NO_TITLE);


        DialogBaseCenterBinding dialogBaseCenterBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.dialog_base_center, null, false);
//        dialogBaseCenterBinding.getRoot().setBackgroundColor(Color.parseColor("#80ff0000"));
        dialogViewBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), setUpLayoutId(), null, false);
        setSizeW(BaseVBDialog.getScreenWidthDp(mContext));
        setSizeH(-1);

        dialogBaseCenterBinding.dialogContent.addView(dialogViewBinding.getRoot());

        setContentView(dialogBaseCenterBinding.getRoot());


        doAction();
    }


    @Override
    protected void onStart() {
        super.onStart();
        initParams();
    }

    private int bgColor = Color.TRANSPARENT;

    public void setBackgroundColor(int _bgColor) {
        bgColor = _bgColor;
    }

    private void initParams() {
        Window window = getWindow();
        if (window != null) {
            window.setBackgroundDrawable(new ColorDrawable(bgColor));
            WindowManager.LayoutParams params = window.getAttributes();
            Log.e("BaseVBDialogxxxx", "initParams  ");
            params.flags = WindowManager.LayoutParams.FLAG_FULLSCREEN | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;

            //设置dialog显示位置
            if (mShowBottomEnable) {
                params.gravity = Gravity.BOTTOM;
            }

            params.copyFrom(window.getAttributes());
            params.width = WindowManager.LayoutParams.MATCH_PARENT;
            params.height = WindowManager.LayoutParams.MATCH_PARENT;

//            //设置dialog宽度
//            if (mWidth == 0) {
//                params.width = getScreenWidth(getContext()) - 2 * dp2px(getContext(), mMargin);
//            } else {
//                params.width = dp2px(getContext(), mWidth);
//            }
//
//            //设置dialog高度
//            if (mHeight == 0) {
//                params.height = WindowManager.LayoutParams.WRAP_CONTENT;
//            } else if (mHeight == -1) {
//                params.height = getScreenHeight(mContext);
//            } else {
//                params.height = dp2px(getContext(), mHeight);
//            }

            //设置dialog动画
            if (mAnimStyle != 0) {
                window.setWindowAnimations(mAnimStyle);
            }

            window.setAttributes(params);
        }
        setCancelable(mOutCancel);
    }


    public CenterBaseVBDialog setFullScreen(boolean fullScreen) {
        isFullscreen = fullScreen;
        return this;
    }


    /**
     * 设置背景昏暗度
     *
     * @param dimAmount
     * @return
     */
    public CenterBaseVBDialog setDimAmount(@FloatRange(from = 0, to = 1) float dimAmount) {
        mDimAmount = dimAmount;
        return this;
    }

    /**
     * 是否显示底部
     *
     * @param showBottom
     * @return
     */
    public CenterBaseVBDialog setShowBottom(boolean showBottom) {
        mShowBottomEnable = showBottom;
        return this;
    }

    /**
     * 设置宽高 单位dp
     *
     * @param width
     * @param height
     * @return
     */
    public CenterBaseVBDialog setSize(int width, int height) {
        mWidth = width;
        mHeight = height;
        return this;
    }

    /**
     * 设置宽 单位dp
     *
     * @param width
     * @return
     */
    public CenterBaseVBDialog setSizeW(int width) {
        mWidth = width;
        return this;
    }

    /**
     * 设置高 单位dp
     *
     * @param height -1 高度占满
     */
    public CenterBaseVBDialog setSizeH(int height) {
        mHeight = height;
        return this;
    }

    /**
     * 设置左右margin
     *
     * @param margin
     * @return
     */
    public CenterBaseVBDialog setMargin(int margin) {
        mMargin = margin;
        return this;
    }

    /**
     * 设置进入退出动画
     *
     * @param animStyle
     * @return
     */
    public CenterBaseVBDialog setAnimStyle(@StyleRes int animStyle) {
        mAnimStyle = animStyle;
        return this;
    }

    /**
     * 设置是否点击外部取消
     *
     * @param outCancel
     * @return
     */
    public CenterBaseVBDialog setOutCancel(boolean outCancel) {
        mOutCancel = outCancel;
        return this;
    }

    @Deprecated
    @Override
    public void show() {
        super.show();
    }


    @Deprecated
    @Override
    public void dismiss() {
        super.dismiss();
    }

    public CenterBaseVBDialog showDialog() {

        super.show();
        if (mContext instanceof Activity && isFullscreen) {
            Log.e("BaseDialog", "mContext instanceof Activity");

            ViewGroup.LayoutParams layoutParams = getWindow().getAttributes();
            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;

            getWindow().getDecorView().setPadding(0, 0, 0, 0);

//            getWindow().setAttributes((WindowManager.LayoutParams) layoutParams);


        }
        return this;
    }

    public CenterBaseVBDialog showDialog(int x, int y) {


        WindowManager.LayoutParams layoutParams = getWindow().getAttributes();
        if (mContext instanceof Activity && isFullscreen) {
            Log.e("BaseDialog", "mContext instanceof Activity");

            layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
            layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;
        }
        layoutParams.x = x;
        layoutParams.y = y;

        getWindow().getDecorView().setPadding(0, 0, 0, 0);

//        getWindow().setAttributes((WindowManager.LayoutParams) layoutParams);
        getWindow().setGravity(Gravity.TOP | Gravity.LEFT);
        super.show();
        return this;
    }

    public void dismissDialog() {
        views.clear();
        super.dismiss();
    }

    /**
     * 设置dialog布局
     *
     * @return
     */
    public abstract int setUpLayoutId();


    /**
     * 获取屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        return displayMetrics.widthPixels;
    }

    public static int getScreenHeight(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Log.e("屏幕高度===>", displayMetrics.heightPixels + "");
        return displayMetrics.heightPixels;
    }

    public static int getScreenHeightDp(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Log.e("屏幕高度===>", displayMetrics.heightPixels + "");
        return (int) px2dp(context, displayMetrics.heightPixels);
    }

    public static int getScreenWidthDp(Context context) {
        DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
        Log.e("屏幕高度===>", displayMetrics.widthPixels + "");
        return (int) px2dp(context, displayMetrics.widthPixels);
    }

    public static int dp2px(Context context, float dipValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        Log.e("转换高度===>", ((int) (dipValue * scale + 0.5f)) + "");
        return (int) (dipValue * scale + 0.5f);
    }

    /**
     * 根据手机的分辨率从 px(像素) 的单位 转成为 dp
     */
    public static float px2dp(Context context, float pxValue) {
        return (pxValue / context.getResources().getDisplayMetrics().density);
    }


}
